<!--_meta 作为公共模版分离出去-->
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <link rel="Bookmark" href="/favicon.ico">
    <link rel="Shortcut Icon" href="/favicon.ico"/>
    <!--[if lt IE 9]-->
    <script type="text/javascript" src="lib/html5shiv.js"></script>
    <script type="text/javascript" src="lib/respond.min.js"></script>
    <![endif]-->
    <link rel="stylesheet" href="lib/select2/select2.min.css">
    <link rel="stylesheet" type="text/css" href="static/h-ui/css/H-ui.min.css"/>
    <link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/H-ui.admin.css"/>
    <link rel="stylesheet" type="text/css" href="lib/Hui-iconfont/1.0.8/iconfont.css"/>
    <link rel="stylesheet" type="text/css" href="static/h-ui.admin/skin/default/skin.css" id="skin"/>
    <link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/style.css"/>
    <!--<link rel="stylesheet" type="text/css" href="http://cdn.amazeui.org/amazeui/2.7.2/css/amazeui.css"/>-->

    <!--[if IE 6]-->
    <script type="text/javascript" src="lib/DD_belatedPNG_0.0.8a-min.js"></script>
    <script>DD_belatedPNG.fix('*');</script>
    <![endif]-->
    <!--/meta 作为公共模版分离出去-->
    <title>一键导入</title>

</head>
<body>
<nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 接口管理 <span
        class="c-gray en">&gt;</span> swagger导入 <a class="btn btn-success radius r"
                                                   style="line-height:1.6em;margin-top:3px"
                                                   href="javascript:location.replace(location.href);" title="刷新"><i
        class="Hui-iconfont">&#xe68f;</i></a></nav>
<div class="page-container">
    <form method="post" class="form form-horizontal" id="form-admin-role-add">
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>服务：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <span class="select-box">
                    <select class="select" size="1" tiserviceDetailId="" name="serviceid" id="serviceid"
                            onchange="setMatchValue()">
                        <option value="" selected>请选择服务</option>
                    </select>
				</span>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>phpDocUrl：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="" readonly id="swaggerUrl" name="swaggerUrl"
                       style="background-color: #DEDEDE;">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>是否覆盖：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <label for="overwrite_true" title="接口地址相同时则更新">
                    <input id="overwrite_true" type="radio" value="true" name="cover">是
                </label>&emsp;
                <label for="overwrite_false" title="接口地址相同时忽略">
                    <input id="overwrite_false" type="radio" checked value="false" name="cover">否
                </label>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>是否删除：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <label for="delete_true" title="swagger中不存在的历史接口将被删除">
                    <input id="delete_true" type="radio" value="true" name="isDelete">是
                </label>&emsp;
                <label for="delete_false" title="swagger中不存在的历史接口保留">
                    <input id="delete_false" type="radio" checked value="false" name="isDelete"
                    >否
                </label>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3">接口开发者：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <span class="select-box">
                    <select class="select" size="1" name="iDev" id="iDev">
                        <option value="" selected>请选择</option>
                    </select>
				</span>
            </div>
        </div>
        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <button type="button" class="btn btn-success radius" id="admin-role-save" name="admin-role-save"
                        onclick="importInterfaces()"><i class="icon-ok"></i> 导入
                </button>
                <button type="button" class="btn btn-success radius" id="admin-role-cancel" name="admin-role-cancel"
                        onclick="empty()"><i class="icon-ok"></i> 重置
                </button>
            </div>
        </div>
    </form>
</div>

<div class="page-container" id="importResult" style="display: none">
    <form method="" class="form form-horizontal">
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><a href="javascript:;" class="btn btn-primary radius ">导入成功！</a></label>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3">服务ID：</label>
            <p id="serviceIdResult"></p>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3">服务名：</label>
            <p id="serviceNameResult"></p>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3">总接口数：</label>
            <p id="iCountResult"></p>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3">导入明细：</label>
            <div style="width: 75%;margin-left: 15%;" id="DataTables_Table_0_wrapper"
                 class="dataTables_wrapper no-footer">
                <table class="table table-border table-bordered table-hover table-bg table-sort">
                    <thead>
                    <tr class="text-c">
                        <th width="25%" id="insertCount">新增接口</th>
                        <th width="25%" id="updateCount">更新接口</th>
                        <th width="25%" id="failCount">失败接口</th>
                        <th width="25%" id="deleteCount">删除接口</th>
                    </tr>
                    </thead>
                    <tbody id="importList">
                    </tbody>
                </table>
            </div>
        </div>
    </form>
</div>

<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src="lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="lib/layer/2.4/layer.js"></script>
<script type="text/javascript" src="static/h-ui/js/H-ui.min.js"></script>
<script type="text/javascript" src="static/h-ui.admin/js/H-ui.admin.js"></script> <!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="lib/select2/select2.full.js"></script>
<script type="text/javascript" src="lib/select2/pinyin.js"></script>
<script type="text/javascript" src="lib/jquery.validation/1.14.0/jquery.validate.js"></script>
<script type="text/javascript" src="lib/jquery.validation/1.14.0/validate-methods.js"></script>
<script type="text/javascript" src="lib/jquery.validation/1.14.0/messages_zh.js"></script>

<script type="text/javascript">

    var seditorSet = null;
    var id;

    function setMatchValue() {
        //debugger;
        var ele = $("#serviceid").select2('data')[0].element;
        var url = $(ele).data('url');
        id = $(ele).data('id');
        $("#swaggerUrl").val(url);
        var editor = $(ele).data('editor');
        set_select_checked("iDev", editor);
    }

    function set_select_checked(selectId, checkValue) {
        var select = $("#" + selectId).select2();    //获取selectid
        select.val(checkValue).trigger("change");    //设置 value 为four的 option 为选中状态
        select.change();//告诉select2代码已经更新，需要重载  默认选择有值得第一个
    }

    function importInterfaces() {
        clearResult();
        //debugger;
        var serviceId = $("#serviceid").val().split("&")[0];
        var envId = $("#serviceid").val().split("&")[1];
        var overwrite = $('input[name="cover"]:checked').val();
        var isDelete = $("input[name='isDelete']:checked").val();
        var iDev = $("#iDev").val();
        var serviceDetailId = document.getElementById("serviceid").getAttribute("tiserviceDetailId");

        if (serviceId == null || serviceId == "") {
            layer.msg("请选择服务！");
            return;
        } else {
            var layerIndex = layer.load(1, {shade: [1.01, '#fff']});
            $.ajax({
                type: "post",
                url: "/import/interfaceImportFromPhpDoc",
                data: {
                    serviceDetailId: id,
                    overwrite: overwrite,
                    isDelete: isDelete,
                    iDev: iDev
                },
                dataType: "json",
                success: function (data) {
                    layer.close(layerIndex); //完成加载后关闭loading
                    var status = data.status;
                    var msg = data.msg;
                    if (status == 0) {
                        displayResult(data.data);
                    } else if (msg == null) {
                        layer.alert("未知异常", {
                            icon: 0,
                            skin: 'layer-ext-moon'
                        });
                    } else {
                        layer.alert(data.msg, {
                            icon: 0,
                            skin: 'layer-ext-moon'
                        });
                    }
                },
                fail: function (data) {
                    layer.close(layerIndex); //完成加载后关闭loading
                    layer.alert(JSON.stringify(data), {
                        icon: 0,
                        skin: 'layer-ext-moon'
                    });
                },
                error: function (xhr) {
                    // report error
                    layer.close(layerIndex);
                    layer.alert('Error' + JSON.stringify(xhr), {
                        icon: 2,
                        skin: 'layer-ext-moon'
                    })
                }
            });
        }

    }

    function clearResult() {
        $("#importResult").hide();
        $("#serviceIdResult").text("");
        $("#serviceNameResult").text("");
        $("#iCountResult").text("");
        $("#importList").html("");
    }

    //重置按钮
    function empty() {
        $("#serviceid").val(null).trigger("change");
        $("#swaggerUrl").val("");
        $("#iDev").val(null).trigger("change");
        $("#overwrite_false").prop("checked", true);

        $("#importResult").hide();
        $("#serviceIdResult").text("");
        $("#serviceNameResult").text("");
        $("#iCountResult").text("");
        $("#importList").html("");
    }

    function displayResult(data) {
        $("#importResult").show();
        $("#serviceIdResult").text(data.serviceId);
        $("#serviceNameResult").text(data.serviceName);
        $("#iCountResult").text(data.totalCount);
        $("#insertCount").text("新增接口(" + data.insertCount + ")");
        $("#updateCount").text("更新接口(" + data.updateCount + ")");
        $("#failCount").text("失败接口(" + data.failCount + ")");
        $("#deleteCount").text("删除接口(" + data.deleteCount + ")");

        var maxRow = 0;
        var html = "";
        maxRow = data.insertCount > data.updateCount ? data.insertCount : data.updateCount;
        maxRow = maxRow > data.failCount ? maxRow : data.failCount;
        maxRow = maxRow > data.deleteCount ? maxRow : data.deleteCount;
        for (var i = 0; i < maxRow; i++) {
            var insertItem = data.insertList[i] == undefined ? "" : data.insertList[i];
            var updateItem = data.updateList[i] == undefined ? "" : data.updateList[i];
            var failItem = data.failList[i] == undefined ? "" : data.failList[i];
            var deleteItem = data.deleteList[i] == undefined ? "" : data.deleteList[i];
            var rowHtml = "<tr class=\"text-c\">\n" +
                "<td style=\"text-align:left;\">" + insertItem + "</td>\n" +
                "<td style=\"text-align:left;\">" + updateItem + "</td>\n" +
                "<td style=\"text-align:left;\">" + failItem + "</td>\n" +
                "<td style=\"text-align:left;\">" + deleteItem + "</td>\n" +
                "</tr>";
            html = html + rowHtml;
        }
        $("#importList").append(html);
    }

    $(document).ready(function () {
        //获取服务环境列表
        $.ajax({
            type: "post",
            url: "/tservicedetail/selectTservicedetailCustomListByCondition",
            dataType: "json",
            success: function (data) {
                var status = data.status;
                var arrayList = data.data;
                if (status == 0) {
                    var html = "";
                    seditorSet = new Set();
                    for (var i in arrayList) {
                        var row = arrayList[i];
                        seditorSet.add(row.seditor);
                        if (row.swaggerurl == "" || row.swaggerurl == null) {
                            var rowHtml = "<option value=" + row.serviceid + "&" + row.envid + " data-url=" + null + " data-editor=" + row.seditor + " data-id=" + row.id + ">" + row.servicekey + "(" + row.envkey + ")</option>";
                        } else {
                            var rowHtml = "<option value=" + row.serviceid + "&" + row.envid + " data-url=" + row.swaggerurl + " data-editor=" + row.seditor + " data-id=" + row.id + ">" + row.servicekey + "(" + row.envkey + ")</option>";
                        }
                        html = html + rowHtml;
                    }
                    $("#serviceid").append(html);
                    $("#serviceid").select2();
                } else {
                    layer.alert(JSON.stringify(data), {
                        icon: 0,
                        skin: 'layer-ext-moon'
                    });
                }
            },
            fail: function (data) {
                layer.alert(JSON.stringify(data), {
                    icon: 0,
                    skin: 'layer-ext-moon'
                });
            },
            error: function (xhr) {
                layer.alert('Error' + JSON.stringify(xhr), {
                    icon: 2,
                    skin: 'layer-ext-moon'
                })
            }
        });
        //获取开发人员列表
        $.ajax({
            type: "post",
            url: "/user/selectDever ",
            data: {},
            dataType: "json",
            success: function (data) {
                var status = data.status;
                var msg = data.msg;
                if (status == 0) {
                    var dev = data.data;
                    var htmldev = "";
                    for (var index in seditorSet) {
                        var currentValue = seditorSet[index];
                        var rowHtml = "<option value=\"" + currentValue + "\">" + currentValue + "</option>";
                        htmldev = htmldev + rowHtml;
                    }
                    for (var i = 0; i < dev.length; i++) {
                        var row = dev[i];
                        var isExist = false;
                        for (var index in seditorSet) {
                            if (row.realname == seditorSet[index]) {
                                isExist = true;
                                return;
                            }
                        }
                        if (!isExist) {
                            var rowHtml = "<option value=\"" + row.realname + "\">" + row.realname + "</option>";
                            htmldev = htmldev + rowHtml;
                        }
                    }
                    $("#iDev").append(htmldev);
                    $("#iDev").select2();
                } else {
                    layer.alert(msg, {
                        icon: 0,
                        skin: 'layer-ext-moon'
                    })
                }
            },
            fail: function (data) {
                layer.alert(JSON.stringify(data), {
                    icon: 0,
                    skin: 'layer-ext-moon'
                });
            },
            error: function (xhr) {
                layer.alert('Error' + JSON.stringify(xhr), {
                    icon: 2,
                    skin: 'layer-ext-moon'
                })
            }
        });
    });

</script>

</body>
</html>